//
//  Registe.m
//  视图控制器
//
//  Created by 18_李明明 on 14-11-15.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Registe.h"
#import "LabelAndTextField.h"
#import "Login.h"
#import "Rootcontain.h"
@interface Registe ()

@end

@implementation Registe

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册";
    // 创建用户名
     self.view.backgroundColor = [UIColor whiteColor];
    LabelAndTextField *userName = [[LabelAndTextField alloc] initWithFrame:CGRectMake(40, 70, 300, 40)];
    userName.myLabel.text = @"用户名";
    userName.myTextField.placeholder = @"请输入用户名";
    [self.view addSubview:userName];
    [userName release];
    userName.myTextField.tag = 100;
    userName.myTextField.delegate = self;
    
    // 创建密码
    LabelAndTextField *passwd = [[LabelAndTextField alloc] initWithFrame:CGRectMake(40, 130, 300, 40)];
    passwd.myLabel.text = @"密码";
    passwd.myTextField.placeholder = @"请输入密码";
    [self.view addSubview:passwd];
    passwd.myTextField.secureTextEntry = YES;
    [passwd release];
    passwd.myTextField.tag = 200;
    passwd.myTextField.delegate = self;
    
    // 确认密码
    LabelAndTextField *confirm = [[LabelAndTextField alloc] initWithFrame:CGRectMake(40, 190, 300, 40)];
    confirm.myLabel.text = @"确认密码";
    confirm.myTextField.placeholder = @"再次输入密码";
    [self.view addSubview:confirm];
    confirm.myTextField.secureTextEntry = YES;
    [confirm release];
    confirm.myTextField.tag = 300;
    confirm.myTextField.delegate = self;
    
    // 手机号
    LabelAndTextField *phone = [[LabelAndTextField alloc] initWithFrame:CGRectMake(40, 250, 300, 40)];
    phone.myLabel.text = @"手机号";
    phone.myTextField.placeholder = @"请输入邮箱";
    [self.view addSubview:phone];
    phone.myTextField.keyboardType = UIKeyboardTypeNumberPad;
    [phone release];
    phone.myTextField.tag = 400;
    
    
    // 邮箱
    LabelAndTextField *mail = [[LabelAndTextField alloc] initWithFrame:CGRectMake(40, 310, 300, 40)];
    mail.myLabel.text = @"邮箱";
    mail.myTextField.placeholder = @"请输入联系方式";
    [self.view addSubview:mail];
    mail.myTextField.keyboardType = UIKeyboardTypeEmailAddress;
    [mail release];
    mail.myTextField.tag = 500;
    mail.myTextField.delegate = self;
    
    // 注册按钮
    UIButton *registe = [UIButton buttonWithType:UIButtonTypeSystem];
    [registe setTitle:@"注册" forState:UIControlStateNormal];
    registe.frame = CGRectMake(80, 390, 40, 30);
    [self.view addSubview:registe];
    [registe addTarget:self action:@selector(targetRegiste:) forControlEvents:UIControlEventTouchUpInside];

    // 取消按钮
    UIButton *cancle = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancle setTitle:@"取消" forState:UIControlStateNormal];
    cancle.frame = CGRectMake(150, 390, 40, 30);
    [self.view addSubview:cancle];
    [cancle addTarget:self action:@selector(targetC:) forControlEvents:UIControlEventTouchUpInside];
    
}
// 取消按钮事件

- (void)targetC:(UIButton *)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)targetCancle:(UIButton *)cancle
{
    Login *login = [[Login alloc] init];
    [self.parentViewController addChildViewController:login];
    [self.view removeFromSuperview];
    [self.parentViewController.view addSubview:login.view];
    [login release];


}

// 注册按钮事件
- (void)targetRegiste:(UIButton *)registe
{
   
    UITextField *username = (UITextField *)[self.view viewWithTag:100];
    UITextField *passwd = (UITextField *)[self.view viewWithTag:200];
    UITextField *confirm = (UITextField *)[self.view viewWithTag:300];
    if (username.text.length != 0 && passwd.text.length != 0) {
        if ([passwd.text isEqual:confirm.text]) {
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert initWithTitle:@"提示" message:@"注册成功" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [alert release];
//            [self dismissViewControllerAnimated:YES completion:^{
//            }];
            
//            Rootcontain *root = [[Rootcontain alloc]init];
//            Registe *reg = [[Registe alloc] init];
            
            
            Login *login = [[Login alloc] init];
            [self.view removeFromSuperview];
            [self.parentViewController.view addSubview:login.view];
            [self.parentViewController addChildViewController:login];
            [login release];
        }else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"两次输入的密码不同,请重新输入" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
            [alert release];
        }
        
    }else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码和用户名不能为空" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        [alert release];
    }
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textfile
{
    // 点击return键让textfield回收键盘
    [textfile resignFirstResponder];
    return YES;
}
// 触摸空白处让键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITextField *username = (UITextField *)[self.view viewWithTag:100];
    UITextField *passwd = (UITextField *)[self.view viewWithTag:200];
    UITextField *confirm = (UITextField *)[self.view viewWithTag:300];
    UITextField *phone =  (UITextField *)[self.view viewWithTag:400];
    UITextField *mail = (UITextField *)[self.view viewWithTag:500];
    [passwd resignFirstResponder];
    [confirm resignFirstResponder];
    [phone resignFirstResponder];
    [mail resignFirstResponder];
    [username resignFirstResponder];
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
